package com.yc.damai.service.impl;

import com.yc.damai.entity.Orderitem;
import com.yc.damai.entity.Orders;
import com.yc.damai.mapper.OrdersMapper;
import com.yc.damai.service.IOrderitemService;
import com.yc.damai.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2022-11-01
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Resource
    private IOrderitemService orderitemService;

    @Override
    public void saveOrder(Orders orders) {
        // 字段验证省略
        // 保存订单
        orders.setOrdertime(LocalDateTime.now());
        orders.setState(1);
        this.save(orders);
        // 保存订单明细
        orders.getOrderitems().forEach(i->{
            // mybatis会自动返回订单id
            i.setOid(orders.getOid());
            orderitemService.save(i);
        });
    }
}
